<template>
  <div class="region">
    <div class="content">
      <div class="left">地区:</div>
      <ul>
        <li
          :class="{ active: RegionFalg === '' }"
          @click="changeRegion('')"
        >全部</li>
        <li
          v-for="item in RegionArr"
          :key="item.id"
          @click="changeRegion(item.value)"
          :class="{ active: RegionFalg === item.value }"
        >{{ item.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reqHospitalLevelAndRegion } from '@/api/home';
import type { HospitalLevelAndRegionArr, HospitalLevelAndRegionResponseData } from '@/api/home/type';
import { onMounted, ref } from 'vue';

// 存储地区数据
let RegionArr = ref<HospitalLevelAndRegionArr>([])
// 地区高亮的响应式数据
let RegionFalg = ref<string>('')
// 地区组件挂载完毕获取地区数据
onMounted(() => {
  getRegion()
})
// 获取医院地区的数据
const getRegion = async () => {
  let result: HospitalLevelAndRegionResponseData = await reqHospitalLevelAndRegion('Beijin');
  // console.log(result);
  // 存储全部信息的数据
  RegionArr.value = result.data

}
// 点击不同区域按钮的回调
const changeRegion = (region: string) => {
  RegionFalg.value = region
  // 给父组件传递区域的参数
  $emit('getRegion', region);
}

let $emit = defineEmits(['getRegion']);
</script>

<script lang="ts">
export default {
  name: 'Region'
}
</script>

<style scoped lang="scss">
.region {
  margin-top: 10px;
  color: #7f7f7f;

  .content {
    display: flex;

    .left {
      margin-right: 10px;
      width: 38px;
    }

    ul {
      display: flex;
      flex-wrap: wrap;

      li {
        margin-right: 10px;
        margin-bottom: 10px;

        &.active {
          color: #55a6fe;
        }
      }

      li:hover {
        color: #55a6fe;
      }
    }
  }
}
</style>